import {AfterViewInit, Component, OnDestroy, OnInit} from '@angular/core';
import {Message} from '../../../../../ng-tools/src/message/message';
import {Subscription} from 'rxjs';
import {DvbConfigService} from '../../../../services/api/dvb-config.service';
import {MessageService, ProgressBarService, WebsocketService} from '../../../../../ng-tools';

@Component({
  selector: 'third-tools',
  templateUrl: './third-tools.component.html',
  styleUrls: ['./third-tools.component.scss'],
  providers: [WebsocketService]
})
export class ThirdToolsComponent implements OnInit, AfterViewInit, OnDestroy, Message {
  subscriptions: Subscription;
  pingSocket;
  scanSocket;
  interval;
  url;
  pingText = '确认';
  scanText = '扫描';
  pingPostMessage = {'data': ''};
  scanPostMessage = {'data': ''};
  pingResultData = [];
  scanResultData = [];
  isScanning = false;
  breadcrumb = [
    {name: '使用工具', cssIcon: 'fa fa-fw fa-task'},
    {name: '三方工具', cssIcon: 'fa fa-fw fa-task'}
  ];
  constructor(private configService: DvbConfigService,
              private message: MessageService,
              public progress: ProgressBarService,
              private socket: WebsocketService) { }

  ngOnInit() {
    this.configService.get(this.configService.urls.is_scan_channel).subscribe((resp) => {
      console.log(resp.body);
      this.isScanning = resp.body['msg'];
      if (this.isScanning) {
        this.scanText = '停止';
        this.scanPostMessage = {'data': ''};
        this.getScanDataBySocket();
      }
    });
  }
  pingUrl() {
    if (this.pingText === '确认') {
      this.pingText = '停止';
      this.pingPostMessage = {'data': this.url};
      if (this.url === '') {
        alert('请输入地址或域名!');
        return;
      }
      this.getPingDataBySocket();
    }
    if (this.pingText === '停止') {
      if (this.pingSocket && this.pingSocket.readyState === 1) {
        this.pingText = '确认';
        this.pingResultData = [];
        this.pingSocket.close();
      }
    }
  }
  urlChange(event) {
    this.url = event.srcElement.value;
  }
  getScanDataBySocket() {
    this.configService.configObserver().subscribe((config) => {
      this.scanSocket = this.socket.socket(config.baseWebSocketUrl + this.configService.urls.scanChannel);
      this.scanSocket.onmessage = (evt) => {
        this.scanPostMessage = {'data': ''};
        const data = JSON.parse(evt.data);
        if (this.scanResultData.length < 10) {
          this.scanResultData.push(data);
        } else {
          this.scanResultData.shift();
          this.scanResultData.push(data);
        }
      };
      this.scanSocket.onopen = () => {
        this.socket.send(this.scanSocket, this.scanPostMessage);
        this.scanPostMessage = {'data': ''};
        this.interval = this.socket.interval(this.scanSocket, 1000, this.scanPostMessage);
      };
      this.scanSocket.onclose = () => {
        clearInterval(this.interval);
      };
    });
  }
  getPingDataBySocket() {
    this.configService.configObserver().subscribe((config) => {
      this.pingSocket = this.socket.socket(config.baseWebSocketUrl + this.configService.urls.ping);
      this.pingSocket.onmessage = (evt) => {
        this.pingPostMessage = {'data': ''};
        const data = JSON.parse(evt.data);
        if (this.pingResultData.length < 10) {
          this.pingResultData.push(data);
        } else {
          this.pingResultData.shift();
          this.pingResultData.push(data);
        }
      };
      this.pingSocket.onopen = () => {
        this.socket.send(this.pingSocket, this.pingPostMessage);
        this.pingPostMessage = {'data': ''};
        this.interval = this.socket.interval(this.pingSocket, 1000, this.pingPostMessage);
      };
      this.pingSocket.onclose = () => {
        clearInterval(this.interval);
      };
    });
  }
  release() {
    if (this.pingSocket && this.pingSocket.readyState === 1) {
      this.pingSocket.close();
    }
  }
  scanChannel() {
    if (this.scanText === '扫描') {
      this.scanText = '停止';
      this.scanPostMessage = {'data': 'start'};
      this.getScanDataBySocket();
      // }
    }
    if (this.scanText === '停止') {
      if (this.scanSocket && this.scanSocket.readyState === 1) {
        this.scanText = '扫描';
        this.scanResultData = [];
        this.scanSocket.close();
      }
    }
  }
  ngAfterViewInit(): void {
    this.message.sendMessage('breadcrumb changed', this, this.breadcrumb);
  }
  ngOnDestroy(): void {
    this.release();
  }

}
